package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.system.domain.LEmployee;
import com.ruoyi.system.domain.LGuest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.LRegion;
import com.ruoyi.system.service.ILRegionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 区域Controller
 *
 * @author zyb
 * @date 2022-01-03
 */
@RestController
@RequestMapping("/system/region")
public class LRegionController extends BaseController {
    @Autowired
    private ILRegionService lRegionService;
    @Autowired
    private RedisCache redisCache;

    /**
     * 查询区域列表
     */
    @PreAuthorize("@ss.hasPermi('system:region:list')")
    @GetMapping("/list")
    public TableDataInfo list(LRegion lRegion) {
        startPage();
        List<LRegion> list = lRegionService.selectLRegionList(lRegion);
        return getDataTable(list);
    }

    /**
     * 导出区域列表
     */
    @PreAuthorize("@ss.hasPermi('system:region:export')")
    @Log(title = "区域", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LRegion lRegion) {
        List<LRegion> list = lRegionService.selectLRegionList(lRegion);
        ExcelUtil<LRegion> util = new ExcelUtil<LRegion>(LRegion.class);
        util.exportExcel(response, list, "区域数据");
    }

    /**
     * 获取区域详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:region:query')")
    @GetMapping(value = "/{areaid}")
    public AjaxResult getInfo(@PathVariable("areaid") Long areaid) {
        return AjaxResult.success(lRegionService.selectLRegionByAreaid(areaid));
    }

    /**
     * 新增区域
     */
    @PreAuthorize("@ss.hasPermi('system:region:add')")
    @Log(title = "区域", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LRegion lRegion) {
        redisCache.deleteObject("lRegion");
        return toAjax(lRegionService.insertLRegion(lRegion));
    }

    /**
     * 修改区域
     */
    @PreAuthorize("@ss.hasPermi('system:region:edit')")
    @Log(title = "区域", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LRegion lRegion) {
        redisCache.deleteObject("lRegion");
        return toAjax(lRegionService.updateLRegion(lRegion));
    }

    /**
     * 删除区域
     */
    @PreAuthorize("@ss.hasPermi('system:region:remove')")
    @Log(title = "区域", businessType = BusinessType.DELETE)
    @DeleteMapping("/{areaids}")
    public AjaxResult remove(@PathVariable Long[] areaids) {
        redisCache.deleteObject("lRegion");
        return toAjax(lRegionService.deleteLRegionByAreaids(areaids));
    }

    @GetMapping("/list/all")
    public AjaxResult listAll() {
        List<LRegion> list = redisCache.getCacheList("lRegion");
        if (list.size() == 0) {
            LRegion region = new LRegion();
            region.setClosed(0L);
            list = lRegionService.selectLRegionList(region);
            redisCache.setCacheList("lRegion", list);
        }

        return AjaxResult.success(list);
    }

    @GetMapping("/getRegion")
    public AjaxResult getRegion() {
        return AjaxResult.success(lRegionService.getRegion());
    }


}
